﻿//
// eCRAM, a monthly activity report web application
// Copyright (C) 2010  Aptea
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

using System.Data.Linq;
using System.Linq;
using eCRAM.Model;

namespace eCRAM.Dao
{
    /// <summary>
    /// This is a line inside the CRAM.
    /// </summary>
    public class CramLineRepository : LinqRepository<CramLine>
    {
        private Table<CramLine> CramLinesTable { get; set; }

        public CramLineRepository(DataContext dataContent)
            : base(dataContent)
        {
             CramLinesTable = DataContext.GetTable<CramLine>();
        }

        public override IQueryable<CramLine> GetAll()
        {
            return (from cramLine in CramLinesTable select cramLine);
        }

        public override void Create(CramLine cramLine)
        {
            CramLinesTable.InsertOnSubmit(cramLine);
            //SubmitChanges();
        }

        /*public override void Update(CramLine cramLine)
        {
            CramLine tmp = (CramLinesTable.Where(p => p.Id == cramLine.Id)).Single();
            //FIXME tmp.Activity = cramLine.Activity;
        }
         * */

        public override void Delete(CramLine cramLine)
        {
            CramLine originalCramLine = GetById(cramLine.Id);
            if (originalCramLine != null)
            {
               
                CramLinesTable.DeleteOnSubmit(originalCramLine);
                //SubmitChanges();
            }
        }

        /// <summary>
        /// Get CramLine
        /// </summary>
        /// <param name="CramLineId"></param>
        /// <returns></returns>
        public override CramLine GetById(System.Guid cramlineId)
        {
            CramLine cramline = (CramLinesTable.Where(p => p.Id == cramlineId)).FirstOrDefault< CramLine>();
            return cramline;
        }

        /// <summary>
        /// Get only CramLine list from start to limit 
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public override IQueryable<CramLine> GetTop(int start, int end)
        {
            IQueryable<CramLine> cramlines = (
                                       from cram
                                       in CramLinesTable
                                       select cram
                                    )
                                    .Skip(start)
                                    .Take(end - start);
            return cramlines;
        }

        /// <summary>
        /// Get the CramLines Count in db
        /// </summary>
        /// <returns></returns>
        public override int GetCount()
        {

            return (from cram
                       in CramLinesTable
                    select cram).Count();
        }



    }
}
